
package com.yitongmao.util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yitongmao.bean.HomeListBean;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GsonUtils {

    static Gson gson;
    static {
        gson = new Gson();
    }

    public static Gson getGsonInstance(){
        return gson;
    }

    // 1、对象转string
    public static String object2String(Object o) {

        return gson.toJson(o);
    }

    // 2、string转对象
    public static <T> T string2Object(String text, Class<T> clazz) {
        return gson.fromJson(text, clazz);
    }

    // 3、对象List转string
    public static String list2String(List<Object> l) {

        Type type = new TypeToken<List<Object>>() {
        }.getType();
        return gson.toJson(l, type);

    }


    //对象ArrayList转string
    public static String arrayList2String(ArrayList<Column> l) {
        Type type = new TypeToken<ArrayList<Column>>() {
        }.getType();
        return gson.toJson(l, type);

    }

    // 4、string转对象List
    public static Object string2List(String str, Type type) {
        // Type type = new TypeToken<List<Object>>(){}.getType();
        return gson.fromJson(str, type);
    }
    
    
    public static ArrayList<Column> string2arrayList(String str, Class<HomeListBean> homeListBeanClass) {
        Type type1 = new TypeToken<ArrayList<Column>>() {
        }.getType();
        return gson.fromJson(str, type1);
    }

    // 5、 将HashMap字符串转换为 String
    public static String map2String(Map<String, String> m) {
        return gson.toJson(m);
    }

    // 6、 将HashMap字符串转换为 String
    public static String string2Map(String str) {
        Type type = new TypeToken<Map<String, String>>() {
        }.getType();
        return gson.fromJson(str, type);
    }

    //ArrayList<HashMap<String, String>>转string
    public static String arrayListToString(ArrayList<HashMap<String, String>> dataArray) {
        Type type = new TypeToken<ArrayList<HashMap<String, String>>>() {
        }.getType();
        return gson.toJson(dataArray, type);
    }
    //string转ArrayList<HashMap<String, String>>
    public static ArrayList<HashMap<String, String>> stringToArrayList(String jsonString) {
        Type type = new TypeToken<ArrayList<HashMap<String, String>>>() {
        }.getType();
        return gson.fromJson(jsonString, type);
    }

    //string转ArrayList<HashMap<String, String>>
    public static ArrayList<HashMap<String, String>> stringToArrayListHashMap(String jsonString) {
        Type type = new TypeToken<ArrayList<HashMap<String, String>>>() {
        }.getType();
        return gson.fromJson(jsonString, type);
    }

    public static ArrayList<Map<String, String>> string2ArrayListMap(String jsonString){
        Type type = new TypeToken<ArrayList<Map<String, String>>>() {
        }.getType();
        return gson.fromJson(jsonString, type);
    }
}
